package com.thon.service.project.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thon.dao.project.ProjectAttentionDao;
import com.thon.dao.project.ProjectDao;
import com.thon.dao.project.ProjectRecommendDao;
import com.thon.dao.project.ProjectSupportDao;
import com.thon.dao.project.ProjectUnsupportDao;
import com.thon.dao.util.Page;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectCategory;
import com.thon.entity.user.Institution;
import com.thon.service.project.ProjectService;

@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	private ProjectDao projectDao;
	@Autowired
	private ProjectAttentionDao projectAttentionDao;

	@Autowired
	private ProjectRecommendDao projectRecommendDao;
	
	@Autowired
	private ProjectUnsupportDao projectUnsupportDao;
	
	@Autowired
	private ProjectSupportDao projectSupportDao;

	

	
	@Override
	public void addProject(Project project) {

		projectDao.save(project);
	}

	@Override
	public void updateProject(Project project) {
		
		if(project==null)
			return;
		Project oldProject = projectDao.get(project.getId());
		if(oldProject==null)
			return;
		if(project.getName()!=null&&project.getName()!="")
		{
			oldProject.setName(project.getName());
		}
		if(project.getActivityBackground()!=null&&project.getActivityBackground()!="")
		{
			oldProject.setActivityBackground(project.getActivityBackground());
		}
		if(project.getActivityContent()!=null&&project.getActivityContent()!="")
		{
			oldProject.setActivityContent(project.getActivityContent());
		}
		if(project.getActivityObject()!=null&&project.getActivityObject()!="")
		{
			oldProject.setActivityObject(project.getActivityObject());
		}
		if(project.getAddress()!=null&&project.getAddress()!="")
		{
			oldProject.setAddress(project.getAddress());
		}
		if(project.getCityRegion()!=null)
		{
			oldProject.setCityRegion(project.getCityRegion());
		}
		if(project.getContect()!=null)
		{
			oldProject.setContect(project.getContect());
		}
		if(project.getDescription()!=null&&project.getDescription()!="")
		{
			oldProject.setDescription(project.getDescription());
		}
		if(project.getDistrictRegion()!=null)
		{
			oldProject.setDistrictRegion(project.getDistrictRegion());
		}
		if(project.getInstitution()!=null)
		{
			oldProject.setInstitution(project.getInstitution());
		}
		
		if(project.getProvinceRegion()!=null)
		{
			oldProject.setProvinceRegion(project.getProvinceRegion());
		}
		if(project.getIsAttention()!=null)
		{
			oldProject.setIsAttention(project.getIsAttention());
		}
		if(project.getIsRecommended()!=null)
		{
			oldProject.setIsRecommended(project.getIsRecommended());
		}
		if(project.getPhotoId()!=null)
		{
			oldProject.setPhotoId(project.getPhotoId());
		}
		if(project.getProjectBenefit()!=null)
		{
			oldProject.setProjectBenefit(project.getProjectBenefit());
		}
		if(project.getProjectCategory()!=null)
		{
			oldProject.setProjectCategory(project.getProjectCategory());
		}
		if(project.getRequirementAnalysis()!=null&&project.getRequirementAnalysis()!="")
		{
			oldProject.setRequirementAnalysis(project.getRequirementAnalysis());
		}
		if(project.getStatus()!=null)
		{
			oldProject.setStatus(project.getStatus());
		}
		if(project.getUserGender()!=null)
		{
			oldProject.setUserGender(project.getUserGender());
		}
		if(project.getUserPhone()!=null&&project.getUserPhone()!="")
		{
			oldProject.setUserPhone(project.getUserPhone());
		}
		if(project.getUserQQ()!=null&&project.getUserQQ()!="")
		{
			oldProject.setUserQQ(project.getUserQQ());
		}
		if(project.getUserRealName()!=null&&project.getUserRealName()!="")
		{
			oldProject.setUserRealName(project.getUserRealName());
		}		
		projectDao.update(oldProject);
	}

	@Override
	public void deleteProject(Integer id) {

	}
	
	@Override
	public List<Project> listProjectByStatus(int status) {
		List<Project> projects = new ArrayList<Project>();
		projects = projectDao.listProjectByStatus(status);
		for (Project project : projects) {
			if(project.getStatus()==3){
				setRemainingDay(project);
			}
		}
		return projects;
	}

	@Override
	public Project getProject(int userId, int id) {

		Project project = projectDao.get(id);
		List<Project> list=new ArrayList<Project>();
		list.add(project);
		hasAttentionAndRecommend(userId, list);
		if(list.get(0).getStatus() == 3){
			setRemainingDay(project);
		}
		Institution institution =  project.getInstitution();
		if(institution!=null){
		Integer supportProjectCount = projectDao.count(
				"ym_project_supports",institution.getUser().getId()).intValue();			
		project.getInstitution().setSupportNum(supportProjectCount);
		Integer startProjectNum = projectDao.count(
				"institute_id",institution.getId()).intValue();;
		project.getInstitution().setStartNum(startProjectNum);
		}
		
		Integer supportCount = projectDao.count(
				"supportCount", project.getId()).intValue();
		project.setSupportCount(supportCount);
		project.setUnSupportCount(projectUnsupportDao.getUnSupportCount(id));
		
		return project;
	}


	// 如果没有userId,userId = null
	@Override
	public Page<Project> findProjects(int userId, Integer status,
			int pageNo, int pageSize) {
		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page = projectDao.findProjectPage(page, status);
		hasAttentionAndRecommend(userId, page.getResult());
		for (Project project : page.getResult()) {
			if(project.getStatus() == 3){
				setRemainingDay(project);
			}
		}
		page.asc("remainingDay");
		return page;
	}

	@Override
	public Page<Project> findProjectByAction(int userId, String action,int status,
			int pageNo, int pageSize) {

		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		projectDao.findProjectByAction(page, userId, action,status);
		hasAttentionAndRecommend(userId, page.getResult());
		List<Project> results=new ArrayList<Project>();
		for (Project project : page.getResult()) {
			if(project.getStatus() == 3){
				setRemainingDay(project);
			}
			Integer supportCount=projectDao.count("supportCount", project.getId()).intValue();
			project.setSupportCount(supportCount);
			results.add(project);
		}
		page.setResult(results);
		return page;
	}

	@Override
	public Page<Project> findProjectByCategory(int categoryId, int pageNo,
			int pageSize,int userId,int status) {

		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		projectDao.findProjectByCategory(page, categoryId,status);
		hasAttentionAndRecommend(userId, page.getResult());
		for (Project project : page.getResult()) {
			if(project.getStatus() == 3){
				setRemainingDay(project);
			}
		}
		
		return page;
	}

	@Override
	public Long countProject(String by, int status) {
		return projectDao.count(by, status);
	}

	@Override
	public List<Project> listProjectsBy(String by, Object object) {
		return projectDao.listProjectBy(by, object);
	}

	@Override
	public Page<Project> findProjectSort(String by, int pageNo, int pageSize,int userId,int status) {

		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		if (by.equals("latest")) {
			projectDao.findProjectSort(page, "0",status);
			hasAttentionAndRecommend(userId, page.getResult());
			for (Project project : page.getResult()) {
				if(project.getStatus() == 3){
					setRemainingDay(project);
				}
			}
			return page;
		} else if (by.equals("finishes")) {
			projectDao.findProjectSortByRemainDay(page,status);
			hasAttentionAndRecommend(userId, page.getResult());
			for (Project project : page.getResult()) {
				if(project.getStatus() == 3){
					setRemainingDay(project);
				}
			}
			return page;
		} else if (by.equals("unrecommends")) {
			projectDao.findProjectPageByRecommend(page,status);
			hasAttentionAndRecommend(userId, page.getResult());
			for (Project project : page.getResult()) {
				if(project.getStatus() == 3){
					setRemainingDay(project);
				}
			}
			return page;
		} else if (by.equals("views")) {
			projectDao.findProjectSort(page, "1",status);
			hasAttentionAndRecommend(userId, page.getResult());
			for (Project project : page.getResult()) {
				if(project.getStatus() == 3){
					setRemainingDay(project);
				}
			}
			return page;
		} else {
			return null;
		}
	}
	
	@Override
	public Page<Project> findProjectByRegion(int pageNo, int pageSize, int regionId,int status) {
		
		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		
		return projectDao.findProjectByRegion(page, regionId,status);
	}
	
	@Override
	public Page<Project> findAllProject(int status,Integer userId,int pageNo, int pageSize,String queryStr) {
		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page = projectDao.findAllProjects(status,page,queryStr);
		for(Project project:page.getResult()){
			setRemainingDay(project);
		}
		hasAttentionAndRecommend(userId, page.getResult());
		return page;
	}
	
	public void hasAttentionAndRecommend(int userId,List<Project> list){
		if(list!=null)			
		if (userId > 0 ) {
			for (Project project : list) {
				project.setIsAttention(projectAttentionDao.hasAttention(userId,project.getId()).intValue());
				project.setIsRecommended(projectRecommendDao.hasRecommend(userId,
						project.getId()).intValue());
			}
		}else{
			for (Project project : list) {
				project.setIsAttention(0);
				project.setIsRecommended(0);
			}
		}
	}

	private void setRemainingDay(Project project){	
		//取消剩余天数的设置
		/*if(project.getFundraisingDate()!=null){
			Date today = new Date();
			Long diffDay = null;
			Integer remainingDay  = null;
			diffDay = today.getTime() - project.getFundraisingDate().getTime();
			diffDay = diffDay / 86400000;
			remainingDay = project.getValidDays() - diffDay.intValue();
			project.setRemainingDay(remainingDay);
		}*/
	}

	@Override
	public Page<Project> findProjectByTags(int pageNo, int pageSize, String tags,int userId) {
		
		Page<Project> page = new Page<Project>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page = projectDao.findProjectByTags(page, tags);
		if(page!=null)
		if(page.getResult()!=null)
		{for (Project project : page.getResult()) {
			if(project.getStatus() == 3){
				setRemainingDay(project);
			}
		}
		hasAttentionAndRecommend(userId, page.getResult());
		}
		return page;
	}
	
	@Override
	public Project getProjectByInst(int institutionId, int status) {
		return projectDao.getProjectByInst(institutionId, status);
	}
	
	@Override
	public Project getProject(int projectId) {
		return projectDao.get(projectId);
	}
	
	@Override
	public Long countProjectByStatus(int institutionId, int status) {
		return projectDao.countProjectByStatus(institutionId, status);
	}
	
	@Override
	public List<Project> listProjectsByStatus(int institutionId, int status) {
		return projectDao.listProjectsByStatus(institutionId, status);
	}

	@Override
	public Long countProjectByCategory(int categoryId) {
		
		return projectDao.countProjectByCategory(categoryId);
	}

	@Override
	public List<Project> listAllProjectName() {
		
		return projectDao.listALLProjectName();
	}

	@Override
	public Page<Project> findProjectByCategory(Page<Project> page, int categoryId) {
		
		return projectDao.findProjectByCategory(page, categoryId);
	}

	@Override
	public Page<Project> findProjectByBenefit(Page<Project> page, int benefitId) {
		
		return projectDao.findProjectByBenefit(page, benefitId);
	}

	@Override
	public Page<Project> findProjectByOpen(Page<Project> page, int openId) {
		
		return projectDao.findProjectByOpen(page, openId);
	}

	@Override
	public Map<Integer,Integer> countCategoryAmount(Map<Integer, Integer> newCategoryAmount) {
		List<Project> projects = listProjectsBy("1",1);
		for(Project pro:projects)
		{
			ProjectCategory projectCategory;
			projectCategory = pro.getProjectOpen();
			if(projectCategory!=null)
			{
				newCategoryAmount.put(projectCategory.getId(),newCategoryAmount.get(projectCategory.getId())+1);
			}
			projectCategory = pro.getProjectBenefit();
			if(projectCategory!=null)
			{
				newCategoryAmount.put(projectCategory.getId(),newCategoryAmount.get(projectCategory.getId())+1);
			}
			projectCategory = pro.getProjectCategory();
			if(projectCategory!=null)
			{
				newCategoryAmount.put(projectCategory.getId(),newCategoryAmount.get(projectCategory.getId())+1);
			}
		}
		return newCategoryAmount;
	} 

}
